package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 分页查询商品
     * @param key       关键字搜索
     * @param page      当前页
     * @param rows      页大小
     * @param saleable  是否上下架
     * @return      SPUDTO的分页对象
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> queryGoodsByPage(
            @RequestParam(value="key",required = false)  String key, // 查询条件
            @RequestParam(value="page",required = false,defaultValue = "1")  Integer page, // 当前页码
            @RequestParam(value="rows",required = false,defaultValue = "5")  Integer rows, // 每页显示条数
            @RequestParam(value="saleable",required = false)  Boolean saleable//  是否上下架：是否上架，0下架，1上架
    ){
        return ResponseEntity.ok(goodsService.queryGoodsByPage(key, page, rows, saleable));
    }

    /**
     * 商品保存
     * @return
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 上下架处理
     * @param spuId     spu的id
     * @param saleable  是否上下架，true就是上架，false就是下架
     * @return
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam("id") Long spuId,
            @RequestParam("saleable") Boolean saleable
    ){
        goodsService.updateSaleable(spuId, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据spuid查询spu详情
     * @param id   spu的id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@RequestParam("id") Long id){
        SpuDetailDTO spuDetailDTO = goodsService.querySpuDetailBySpuId(id);
        return ResponseEntity.ok(spuDetailDTO);
    }

    /**
     * 根据spuid查询sku集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuId(@RequestParam("id") Long id){
        List<SkuDTO> skuDTOList = goodsService.querySkuListBySpuId(id);
        return ResponseEntity.ok(skuDTOList);
    }

    /**
     * 商品更新
     */
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuid查询spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        SpuDTO spuDTO = goodsService.querySpuById(id);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据sku的id集合查询sku的集合信息
     * @param ids   sku的id集合
     * @return      sku集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids){
        List<SkuDTO> skuList = goodsService.querySkuListByIds(ids);
        return ResponseEntity.ok(skuList);
    }

    /**
     * 减去库存
     * @param carts 购物车的map：  key=>SkuId,   value => 购买数量
     * @return  没有返回值
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> carts){
        goodsService.minusStock(carts);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
